/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pedagio.util;


import org.hsqldb.Server;

/**
 *
 * @author pedroo
 */
public class DataBaseServer {


    public static final String url = "jdbc:hsqldb:hsql://localhost/pedagio";
    public static final String user     = "sa";
    public static final String password = "";
    public static final Server server = new Server();
    
    private DataBaseServer() {

    }       

    /**
     * Inicia o servidor de banco de dados
     */
    public static void start() {

        if(server.getState() == 1) {
            return;
        }

        server.setDatabaseName(0, "pedagio");
        server.setDatabasePath(0, "banco/pedagio");
        server.setLogWriter(null);
        server.setErrWriter(null);
        server.start();
        

        try {
            Class.forName("org.hsqldb.jdbc.JDBCDriver");
        } catch (Exception e) {
            throw new RuntimeException("[HSQLDB] Driver jdbc não encontrado", e);
        }
    }
        

    /** Para a execução do servidor do banco de dados.*/
    public static void stop()
    {
        // 1 == SERVER_ONLINE
        if(server.getState() == 1)
        {
            System.out.println("shutdown server");
            server.stop();
        }
    }
    
    /**
     * Retorna atual do servidor
     * 1 == SERVER_ONlINE
     */
    public static int getState() {
        if(server == null) {
            return -1;
        }
        return server.getState();
    }


}

